<?php
/**
 * @var $this \yii\web\View
 * @var $dataProvider
 * @var $filterModel
 */

use backend\models\Admin;
use common\models\District;
use common\models\Level;
use common\models\Study;
use yii\db\ActiveQuery;

$this->title = '豫安行后台管理系统';
$this->params['breadcrumb'] = [];
$typeList = \common\models\AdminNotice::getTypeList();
?>
<div class="row">
    <div class="box">
        <?php if (Yii::$app->user->identity->level == \common\models\Level::COMPANY): ?>
        <div class="box-body">
            <h4>本月安全教育情况</h4>
            <?php
                /** @var Admin $adminModel */
                $adminModel = \Yii::$app->user->identity;
                $districtIds = array_merge(
                    array_values(District::recursiveParents($adminModel->district->parent_id)),
                    [$adminModel->relate_id]
                );
                $query = Study::find()
                    ->alias('n')
                    ->select('n.*,l.relate_id')
                    ->joinWith('level l', false)
                    ->joinWith('exam e', false)
                    ->andWhere(['>', 'e.item_count', 0])
                    ->andWhere(['>', 'e.total_score', 0])
                    ->andWhere(['>=', 'n.create_time', date('Y-m-01')])
                    ->andWhere(['n.is_deleted' => 0])
                    ->andWhere(['n.is_disabled' => 0])
                    ->andWhere(['or', ['l.level' => Level::HIGHEST], ['in', 'l.relate_id', $districtIds]]);

                $count = $query->count();
                if ($count) {
                    echo '本月已发布'.$count.'个安全教育。';
                } else {
                    echo  '本月还未发布安全教育，请赶紧发布。';
                }
            ?>
        </div>
        <?php endif;?>
        <div class="box-body">
            <h4>平台通知</h4>
            <?= \yii\widgets\ListView::widget([
                'dataProvider' => $dataProvider,
                'summary' => '',
                'itemOptions' => ['class' => 'row'],
                'itemView' => function ($model, $key, $index, $widget) {
                    return '<div class="col-sm-8"><a href="http://'.str_replace('api', 'backend', $_SERVER['HTTP_HOST']) .'/admin-notice/content?id='.$model->id.'" target="_blank">'.$model->title.'</a></div><div class="col-sm-2">'.substr($model->create_time, 2, 8).'</div>';
                }
            ]);?>
        </div>
    </div>
</div>